package com.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan("com.demo")
@EnableWebMvc
public class AppConfig {

    /**
     * 多部分请求 解释器
     * https://docs.spring.io/spring/docs/5.2.2.RELEASE/spring-framework-reference/web.html#mvc-multipart
     *
     * 默认情况下，在实例化{@link DispatcherServlet}时没有提供这个Bean,需要自己配置。
     * 可以在{@code AbstractAnnotationConfigDispatcherServletInitializer#customizeRegistration(ServletRegistration.Dynamic)}
     * 配置{@link DispatcherServlet}对Multipart进行配置，
     * 如最大文件大小、最大请求大小、filesizeThreshold等，如下：
     * protected void customizeRegistration(ServletRegistration.Dynamic registration) {
     *      // Optionally also set maxFileSize, maxRequestSize, fileSizeThreshold
     *      registration.setMultipartConfig(new MultipartConfigElement("/tmp"));
     * }
     *
     * @return StandardServletMultipartResolver
     */
    // 这个Bean名称必须设置为multipartResolver
    @Bean("multipartResolver")
    public StandardServletMultipartResolver upload() {
        return new StandardServletMultipartResolver();
    }

}
